package com.health_predict_springboot.service.impl;

import com.health_predict_springboot.dao.HealthIndicatorMapper;
import com.health_predict_springboot.pojo.HealthIndicator;
import com.health_predict_springboot.service.HealthIndicatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HealthIndicatorServiceImpl implements HealthIndicatorService {

    @Autowired
    private HealthIndicatorMapper healthIndicatorMapper;

    @Override
    public HealthIndicator getIndicatorById(Long indicatorId) {
        return healthIndicatorMapper.selectByIndicatorId(indicatorId);
    }

    @Override
    public List<HealthIndicator> getIndicatorsByUserId(Long userId) {
        return healthIndicatorMapper.selectByUserId(userId);
    }

    @Override
    public List<HealthIndicator> getAllIndicators() {
        return healthIndicatorMapper.selectAllIndicators();
    }

    @Override
    public int addIndicator(HealthIndicator indicator) {
        // 设置创建时间和更新时间
        Date now = new Date();
        indicator.setSubmitTime(now);
        indicator.setUpdateTime(now);
        
        // 计算BMI
        double bmi = calculateBMI(indicator.getHeight(), indicator.getWeight().doubleValue());
        indicator.setBmi(new BigDecimal(bmi).setScale(2, RoundingMode.HALF_UP));
        
        // 确定BMI分类
        if (bmi < 18.5) {
            indicator.setBmiCat("偏轻");
        } else if (bmi < 24) {
            indicator.setBmiCat("正常");
        } else if (bmi < 28) {
            indicator.setBmiCat("超重");
        } else {
            indicator.setBmiCat("肥胖");
        }
        
        // 确定年龄分组
        String ageGroup = getAgeGroup(indicator.getAge());
        indicator.setAgeGroup(ageGroup);
        
        // 判断指标是否异常
        indicator.setIsAbnormal(judgeAbnormal(indicator));
        
        return healthIndicatorMapper.insertIndicator(indicator);
    }

    @Override
    public int updateIndicator(HealthIndicator indicator) {
        // 设置更新时间
        indicator.setUpdateTime(new Date());
        
        // 重新计算BMI和其他派生字段
        double bmi = calculateBMI(indicator.getHeight(), indicator.getWeight().doubleValue());
        indicator.setBmi(new BigDecimal(bmi).setScale(2, RoundingMode.HALF_UP));
        
        if (bmi < 18.5) {
            indicator.setBmiCat("偏轻");
        } else if (bmi < 24) {
            indicator.setBmiCat("正常");
        } else if (bmi < 28) {
            indicator.setBmiCat("超重");
        } else {
            indicator.setBmiCat("肥胖");
        }
        
        String ageGroup = getAgeGroup(indicator.getAge());
        indicator.setAgeGroup(ageGroup);
        
        indicator.setIsAbnormal(judgeAbnormal(indicator));
        
        return healthIndicatorMapper.updateIndicator(indicator);
    }

    @Override
    public int deleteIndicator(Long indicatorId) {
        return healthIndicatorMapper.deleteIndicator(indicatorId);
    }

    @Override
    public List<HealthIndicator> getIndicatorsByPage(Map<String, Object> params) {
        return healthIndicatorMapper.selectIndicatorsByPage(params);
    }

    @Override
    public int getIndicatorCount(Map<String, Object> params) {
        return healthIndicatorMapper.selectIndicatorCount(params);
    }

    @Override
    public List<HealthIndicator> getIndicatorsByUserIdAndTimeRange(Map<String, Object> params) {
        return healthIndicatorMapper.selectByUserIdAndTimeRange(params);
    }

    @Override
    public List<HealthIndicator> getAbnormalIndicators() {
        return healthIndicatorMapper.selectAbnormalIndicators();
    }

    @Override
    public double calculateBMI(int height, double weight) {
        // BMI = 体重(kg) / 身高(m)^2
        double heightInMeters = height / 100.0;
        return weight / (heightInMeters * heightInMeters);
    }

    @Override
    public int judgeAbnormal(HealthIndicator indicator) {
        // 异常判断逻辑
        // 1. 血压异常：收缩压>=140或舒张压>=90
        if (indicator.getApHi() >= 140 || indicator.getApLo() >= 90) {
            return 1;
        }
        // 2. 胆固醇异常：2或3
        if (indicator.getCholesterol() >= 2) {
            return 1;
        }
        // 3. 葡萄糖异常：2或3
        if (indicator.getGluc() >= 2) {
            return 1;
        }
        // 4. BMI异常：<18.5或>=28
        double bmi = indicator.getBmi().doubleValue();
        if (bmi < 18.5 || bmi >= 28) {
            return 1;
        }
        // 无异常
        return 0;
    }
    
    // 根据年龄获取年龄分组
    private String getAgeGroup(int age) {
        if (age <= 10) {
            return "0-10";
        } else if (age <= 20) {
            return "11-20";
        } else if (age <= 30) {
            return "21-30";
        } else if (age <= 40) {
            return "31-40";
        } else if (age <= 50) {
            return "41-50";
        } else if (age <= 60) {
            return "51-60";
        } else if (age <= 70) {
            return "61-70";
        } else if (age <= 80) {
            return "71-80";
        } else if (age <= 90) {
            return "81-90";
        } else {
            return "90+";
        }
    }
}